﻿namespace Wicresoft.Platform.Processes.Mobile
{
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using System.ServiceModel.DomainServices.Client;
    using Wicresoft.Platform.Processes.Mobile.Web.Services;
    using System.Windows;
    using System;

    /// <summary>
    /// <see cref="Page"/> class to present information about the current application.
    /// </summary>
    public partial class Proceed : Page
    {
        /// <summary>
        /// Creates a new instance of the <see cref="About"/> class.
        /// </summary>
        public Proceed()
        {
            InitializeComponent();

            this.Title = ApplicationStrings.ProceedPageTitle;
        }

        /// <summary>
        /// Executes when the user navigates to this page.
        /// </summary>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void wicresoft_Platform_Processes_MobileDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void button1_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            MobileProcessContext process = new MobileProcessContext();
            //
            process.ExecuteFlow(
                     new Workflows.Mobile.Wicresoft_Platform_Processes_Mobile()
                     {
                         Id = Guid.Parse(idTextBlock.Text),

                         //
                         MobileName = ".",
                         LaunchTime = new DateTime(),
                         //

                         PurchaseBy = purchaseByTextBox.Text,
                         PurchaseComment = purchaseCommentTextBox.Text,
                         PurchaseTime = System.DateTime.Now,

                         LastModifiedBy = purchaseByTextBox.Text,
                         LastModifiedTime = System.DateTime.Now,
                         CurrentState = Workflows.Mobile.ProcessState.Purchase.ToString(),
                     },
                      Workflows.Mobile.ProcessState.Purchase,
                     ExecuteFlow_Completed,
                     null);



        }

        private void ExecuteFlow_Completed(InvokeOperation<Guid> operation)
        {
            if (!operation.HasError)
                MessageBox.Show("Workflow updated successfully with state=" + operation.Value);
            else
            {
                var e = operation.ValidationErrors;
                MessageBox.Show("Fail to update workflow with exception:" + operation.Error);
            }

        }
    }
}